//
//  YSCartItemView.m
//  Yueshi
//
//  Created by Northwan on 15/7/27.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSCartItemView.h"
#import "CalcView.h"
#import "YSPresent.h"

@interface YSCartItemView()
@property (strong, nonatomic) NSTimer *countDownTimer;
@property (strong, nonatomic) UIImageView *iconView;
@property (strong, nonatomic) UIButton *prodStatusBtn;
@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *colorLabel;
@property (strong, nonatomic) UILabel *priceLabel;
@property (strong, nonatomic) UILabel *promotionPriceLabel;
@property (strong, nonatomic) UILabel *promotionTimeLabel;
@property (strong, nonatomic) UILabel *presentLabel;
@property (strong, nonatomic) UIButton *coverStatusBtn;

@end


@implementation YSCartItemView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //商品图片
        UIImageView *iconView = [[UIImageView alloc]init];
        [self addSubview:iconView];
        self.iconView = iconView;
        
        UIButton *prodStatusBtn = [[UIButton alloc]init];
        [prodStatusBtn setBackgroundColor:[UIColor blackColor]];
        [prodStatusBtn setAlpha:0.5];
        [prodStatusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [prodStatusBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [iconView addSubview:prodStatusBtn];
        _prodStatusBtn = prodStatusBtn;
        
        //名字
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textColor = NAME_COLOR;
        nameLabel.font = SYS_FONT(NAME_FONT);
        [nameLabel setNumberOfLines:2];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        //颜色分类
        UILabel *colorLabel = [[UILabel alloc] init];
        colorLabel.textColor = SUB_COLOR;
        colorLabel.font = SYS_FONT(SUB_FONT);
        [self addSubview:colorLabel];
        self.colorLabel = colorLabel;
        
        //现价
        UILabel *priceLabel = [[UILabel alloc] init];
        priceLabel.textColor = ThemeColor;
        priceLabel.font = SYS_FONT(NEWPRICE_FONT);
        [self addSubview:priceLabel];
        self.priceLabel = priceLabel;

        //calcView
        CalcView *calcView = [[CalcView alloc]init];
        [self addSubview:calcView];
        self.calcView = calcView;
        
        //状态
        UIButton  *statusBtn  = [[UIButton alloc]init];
        [statusBtn setBackgroundImage:[UIImage imageNamed:@"btn_choose_normal"] forState:UIControlStateNormal];
        [statusBtn setBackgroundImage:[UIImage imageNamed:@"btn_choose_press"] forState:UIControlStateHighlighted];
        [statusBtn setBackgroundImage:[UIImage imageNamed:@"btn_choose_press"] forState:UIControlStateSelected];
        [self addSubview:statusBtn];
        self.statusBtn = statusBtn;
        
        //状态上面的遮盖
        UIButton *coverStatusBtn = [[UIButton alloc]init];
        [coverStatusBtn setBackgroundColor:[UIColor clearColor]];
        [coverStatusBtn addTarget:self action:@selector(tapStatus:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:coverStatusBtn];
        self.coverStatusBtn = coverStatusBtn;
        
        //抢购价
        UILabel *promotionPriceLabel =  [[UILabel alloc]init];
        [promotionPriceLabel setFont:SYS_FONT(NAME_FONT)];
        [promotionPriceLabel setTextColor:SUB_COLOR];
        [promotionPriceLabel setHidden:YES];
        [self addSubview:promotionPriceLabel];
        self.promotionPriceLabel = promotionPriceLabel;
        
        //抢购时间
        UILabel *promotionTimeLabel =  [[UILabel alloc]init];
        [promotionTimeLabel setFont:SYS_FONT(NAME_FONT)];
        [promotionTimeLabel setTextColor:SUB_COLOR];
        [promotionTimeLabel setHidden:YES];
        [self addSubview:promotionTimeLabel];
        self.promotionTimeLabel = promotionTimeLabel;
        
        //买赠
        UILabel *presentLabel =  [[UILabel alloc]init];
        [presentLabel setFont:SYS_FONT(NAME_FONT)];
        [presentLabel setTextColor:HEXColor(@"#333333")];
        [presentLabel setHidden:YES];
        [self addSubview:presentLabel];
        self.presentLabel = presentLabel;
        
    }
    return self;
}

-(void)setCartItemFrame:(YSCartItemFrame *)cartItemFrame{
    _cartItemFrame = cartItemFrame;
    YSCartProduct *product = cartItemFrame.product;
    
    [self.iconView setImageURLStr:product.prodImageUrl];
    self.iconView.frame = cartItemFrame.iconFrame;
    self.prodStatusBtn.frame = CGRectMake(0, 0, cartItemFrame.iconFrame.size.width, cartItemFrame.iconFrame.size.height);
    if(product.prodStatus == 2){//下架
        [self.prodStatusBtn setHidden:NO];
        [self.prodStatusBtn setTitle:@"过期" forState:UIControlStateNormal];
        
    }else if(product.prodStatus == 3){//售罄
        [self.prodStatusBtn setHidden:NO];
        [self.prodStatusBtn setTitle:@"售罄" forState:UIControlStateNormal];
    }else{
        [self.prodStatusBtn setHidden:YES];
    }
   
    
    [self.nameLabel setText:product.prodName];
    self.nameLabel.frame = cartItemFrame.nameFrame;
    
    [self.colorLabel setText:product.prodBuyLink];
    self.colorLabel.frame = cartItemFrame.colorFrame;
    
    [self.priceLabel setText:[NSString stringWithFormat:@"¥%.1f",product.prodNewPrice]];
    self.priceLabel.frame = cartItemFrame.newPriceFrame;
    
//    self.calcView.frame =CGRectMake(SCREENW-15-self.calcView.width, cartItemFrame.calcY + 5, self.calcView.width, self.calcView.height);
    self.calcView.frame =CGRectMake(SCREENW-15-self.calcView.width, CGRectGetMaxY(cartItemFrame.iconFrame) - self.calcView.height + 5, self.calcView.width, self.calcView.height);
    [self.calcView setNum:product.prodCount];
    
    self.statusBtn.frame = cartItemFrame.statusFrame;
    [self.statusBtn setSelected:product.status];
    
    self.coverStatusBtn.frame = cartItemFrame.coverStatusFrame;
    
    if(product.prodPromotion){
        if(product.prodPromotion.isPanicBuy){//限时抢购
            [_promotionPriceLabel setHidden:NO];
            [_presentLabel setHidden:YES];
            NSString *testStr = @"限时包邮价:";
            //抢购价格
            NSString *formatPriceStr = nil;
            if(product.prodPromotion.whetherShipping){
                formatPriceStr = [NSString stringWithFormat:@"限时包邮价:%.1f",product.prodPromotion.price];
            }else{
                formatPriceStr = [NSString stringWithFormat:@"限时抢购价:%.1f",product.prodPromotion.price];
            }

            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:formatPriceStr];
            [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(testStr.length,formatPriceStr.length-testStr.length)];
            [self.promotionPriceLabel setAttributedText:str];
            self.promotionPriceLabel.frame = cartItemFrame.promotionPriceFrame;
            //抢购时间
            [_promotionTimeLabel setHidden:NO];
            NSString *promotionTime = [NSString stringWithFormat:@"仅剩%@",[TimeUtils getFormatRemainTime:[TimeUtils getRemainTime:product.prodPromotion.endTime]]];
            [self.promotionTimeLabel setText:promotionTime];
            self.promotionTimeLabel.frame = cartItemFrame.promotionTimeFrame;
            _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeLabel) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
        }else if(product.prodPromotion.isPresent){//买赠
            [_presentLabel setHidden:NO];
            [_promotionPriceLabel setHidden:YES];
            [_promotionTimeLabel setHidden:YES];
            _presentLabel.frame = cartItemFrame.presentFrame;
            YSPresent *present = product.prodPromotion.presentList[0];
            NSString  *presentName = [NSString stringWithFormat:@"买就赠:%@",present.name];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:presentName];
            [str addAttribute:NSForegroundColorAttributeName value:ThemeColor range:NSMakeRange(4,present.name.length)];
            [_presentLabel setAttributedText:str];
        }else{
            [_presentLabel setHidden:YES];
            [_promotionPriceLabel setHidden:YES];
            [_promotionTimeLabel setHidden:YES];
        }
    }else{
        [_presentLabel setHidden:YES];
        [_promotionPriceLabel setHidden:YES];
        [_promotionTimeLabel setHidden:YES];
    }
    self.cellHeight = cartItemFrame.cellHeight;
    self.frame = cartItemFrame.frame;
}

-(void)timeLabel{
    NSTimeInterval reMainTime = [TimeUtils getRemainTime:_cartItemFrame.product.prodPromotion.endTime];
    if(reMainTime<=0){
        [_countDownTimer invalidate];
        //在这写倒计时完了之后需要做的事情
    }else{
        NSString *timeStr = [NSString stringWithFormat:@"仅剩%@",[TimeUtils getFormatRemainTime:reMainTime]];
        CGSize timeSize = [timeStr sizeWithFont:SYS_FONT(NAME_FONT)];
        [self.promotionTimeLabel setWidth:timeSize.width];
        [self.promotionTimeLabel setText:timeStr];
    }
}

-(void)tapStatus:(UIButton *)btn{
    BOOL status =  self.statusBtn.selected;
    [self.statusBtn setSelected:!status];
    [_cartItemFrame.product setStatus:self.statusBtn.selected];
    [[NSNotificationCenter defaultCenter]postNotificationName:kCartItemChangeNortif object:self.calcView.path];
}


@end
